using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using ECommerce.Library.Web;
using System.Web.Configuration;
using System.Data.Common;
using System.Configuration;
using System.Data;
using System.Data.SqlTypes;

namespace ECommerce.Library.Data
{
    public abstract class SqlServerDAO : BaseDAO
    {
        protected SqlServerDAO(){}

        public static ConnectionStringSettings CurrentConnectionString
        {
            get
            {
                return WebConfigurationManager.ConnectionStrings[SiteConfigSection.SiteConfiguration.DBServerType];
            }
        }

        protected override DbCommand CreateCommand()
        {
            SqlConnection sqlConnection = null;
            SqlCommand sqlCommand = null;

            if (!string.IsNullOrEmpty(SiteConfigSection.SiteConfiguration.DBServerType))
            {
                if (WebConfigurationManager.ConnectionStrings.Count > 0)
                {
                    ConnectionStringSettings connectionStringSetting = CurrentConnectionString;
                    if (connectionStringSetting != null)
                    {
                        string connectionString = connectionStringSetting.ConnectionString;
                        if (!string.IsNullOrEmpty(connectionString))
                        {
                            sqlConnection = new SqlConnection(connectionString);
                            if (sqlConnection != null)
                            {
                                sqlCommand = new SqlCommand();
                                sqlCommand.Connection = sqlConnection;
                                return sqlCommand;
                            }
                        }
                    }
                }
            }
            return sqlCommand;
        }

        protected void AddCommandParameter(SqlCommand command, string parameterId, SqlDbType sqlType,
            int parameterSize, ParameterDirection parameterDirection, object parameterValue)
        {
            // Validate Parameter Properties
            if (command == null)
                throw (new ArgumentNullException("command"));
            if (string.IsNullOrEmpty(parameterId))
                throw (new ArgumentNullException("parameterId"));
            
            // Add Parameter
            SqlParameter newSqlParam = new SqlParameter();
            newSqlParam.ParameterName = parameterId;
            newSqlParam.SqlDbType = sqlType;
            newSqlParam.Direction = parameterDirection;
            newSqlParam.IsNullable = true;

            if (parameterSize > 0)
                newSqlParam.Size = parameterSize;

            if (parameterValue != null)
            {
                if(sqlType == SqlDbType.DateTime && ((DateTime)parameterValue) == DateTime.MinValue)
                    parameterValue = SqlDateTime.MinValue;

                newSqlParam.Value = parameterValue;
            }
            else
                newSqlParam.SqlValue = DBNull.Value;

            command.Parameters.Add(newSqlParam);
        }

        protected static SqlDbType MapDbType(object value)
        {
            if (value is int)
                return SqlDbType.Int;
            else if (value is string)
                return SqlDbType.NVarChar;
            else if (value is DateTime)
                return SqlDbType.DateTime;
            else if (value is decimal)
                return SqlDbType.Decimal;
            else if (value is char)
                return SqlDbType.Char;
            else if (value is bool)
                return SqlDbType.Bit;
            else
                return SqlDbType.Int;
        }

        protected int GetIdMinValue
        {
            get { return 0; }
        }
    }
}
